import * as mine from "../../api/mine";
import { getNewToken } from "../../utils/http-server";
var appInst =  getApp();

Page({
    data: {
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        canIUseGetUserProfile: false,
        userInfo:{},
        session_key:"",
        isAgree:false
    },
    handle_auto_cancel(){
        wx.navigateBack({
            delta: 1
        });
    },
    getUserProfile(e) {

        let that = this;
        let userInfo = {}

        wx.getUserProfile({
            desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (res) => {

                userInfo = res.userInfo;
                getNewToken()
                    .then((res)=>{
                        that.setData({
                            userInfo:userInfo,
                            session_key:res.data.data.session_key
                        });
                    })

                //let userInfo = res.userInfo;
            }
        })
    },
    getPhoneNumber(e){

        let detail = e.detail;
        
        if(detail.errMsg == "getPhoneNumber:ok" ){

            mine.login({
                "encrypteddata": detail.encryptedData,
                "iv": detail.iv,
                "sessionkey": this.data.session_key,
                ...this.data.userInfo
            })
            .then((res)=>{

                appInst.globalData.pageCanLoad = true;

                wx.navigateBack({
                    delta: 1
                });
            })

            // getNewToken()
            // .then((res)=>{
            //     session_key = res.data.data.session_key;
            //     return mine.login({
            //         "encrypteddata": detail.encryptedData,
            //         "iv": detail.iv,
            //         "sessionkey": session_key,
            //         ...this.data.userInfo
            //     })
            // })
            // .then((res)=>{
            //     wx.navigateBack({
            //         delta: 1
            //     });
            // })
        }
    },
    handle_check_agree(){
        if(!this.data.isAgree){
            wx.showToast({
                title: '请阅读并同意一下协议~',
                icon: 'none',
                image: '',
                duration: 1500,
                mask: true
            });
        }
    },
    jump_to_register(){
        wx.navigateTo({
            url: '/pages/doc-register/doc-register'
        });
    },
    jump_to_privacy(){
        wx.navigateTo({
            url: '/pages/doc-privacy/doc-privacy'
        });
    },
    handle_agreement(){
        this.setData({
            isAgree : !this.data.isAgree
        })
    }
})
